/**
 * @file   LoanAuthCheck.java
 * @date   2012-11-9下午4:11:16
 * @author User
 */
package com.gzceb.zhgjj.filecheck.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gzceb.zhgjj.filecheck.AbstractFileCheck;
import com.gzceb.zhgjj.filecheck.FileCheckConstants;
import com.gzceb.zhgjj.service.LoanAuthService;
import com.gzceb.zhgjj.util.SpringContextUtil;

/**
 * <p>TTF001贷款授权文件校验类。</p>
 *
 * @date   2012-11-9下午4:11:16 
 * @author liqing
 * @description 
 */
public class LoanAuthCheck extends AbstractFileCheck {

	private static Log logger = LogFactory.getLog(LoanAuthCheck.class);
	
	public LoanAuthCheck(){
		
	}
	
	public LoanAuthCheck(File file){
		logger.info("the received file name: " + file.getAbsolutePath());
		this.recvFileName = getRecvFileName(file);
		this.isNamelegal = isFormatedName(this.recvFileName)
				&& isDateFormated(this.recvFileName)
				&& isBankCode(this.recvFileName);
		this.setRespFileName();
		this.fileCont = this.getRecvFileCont(file);
	}
	
	
	/* (non-Javadoc)
	 * @see com.gzceb.zhgjj.filecheck.FileCheck#setGenRecdFlag(java.util.List)
	 */
	@Override
	public void setGenRecdFlag() {
		// TODO Auto-generated method stub
		if (true == this.isNamelegal) {
			if (!isHeadRight(fileCont)) {
				this.genRecdFlag = FileCheckConstants.ERR_FMT.getValue();
				return;
			} else if (Integer.parseInt(fileCont.get(0).toString().substring(0, 6)) != (fileCont
					.size() - 1)) {
				this.genRecdFlag = FileCheckConstants.ERR_MISMATCH.getValue();
				return;
			} else if (alreadExist()) {
				this.genRecdFlag = FileCheckConstants.ERR_ARDRCD.getValue();

			} else {
				this.genRecdFlag = FileCheckConstants.RT_ACCEPT.getValue();
			}

		} else {
			this.genRecdFlag = "0"; // 文件名称不合法
		}
	}

	/* (non-Javadoc)
	 * @see com.gzceb.zhgjj.filecheck.FileCheck#getGenRecdFlag(java.util.List)
	 */
	@Override
	public String getGenRecdFlag() {
		// TODO Auto-generated method stub
		return this.genRecdFlag;
	}

	/* (non-Javadoc)
	 * @see com.gzceb.zhgjj.filecheck.FileCheck#setEachRecdFlag()
	 */
	@Override
	public void setEachRecdFlag() {
		// TODO Auto-generated method stub
		String eachFlag;
		// 如果总记录标志为 0 或者 1，所有分记录标志置为0
		if(this.genRecdFlag == FileCheckConstants.ERR_FMT.getValue() 
				|| this.genRecdFlag == FileCheckConstants.ERR_NAME.getValue()){
			eachFlag = "0";
			logger.debug("the Flag for each line of this file will be set 0: " + this.recvFileName);
			//授权表第一行是总记录，从第二行开始分记录
			for(int i=1; i<this.fileCont.size(); i++){
				this.fileCont.set(i, this.fileCont.get(i) + eachFlag);
			}
		} else{
			//如果总记录是 8， 9：
			for (int i = 1; i<this.fileCont.size(); i++){
				String str = this.fileCont.get(i);
				eachFlag = "0";
				if( !isFormatRtEach(str) ){
					eachFlag = FileCheckConstants.ERR_FMT.getValue(); 
				}else if( alreadExistEach(str)){
					eachFlag = FileCheckConstants.ERR_ARDRCD.getValue();
				}else {
					eachFlag = FileCheckConstants.RT_ACCEPT.getValue();
				}
				this.fileCont.set(i, str + eachFlag);
			}
		}
	}

	/* (non-Javadoc)
	 * @see com.gzceb.zhgjj.filecheck.FileCheck#getEachRecdFlag(java.util.List)
	 */
	@Override
	public String getEachRecdFlag(List<String> fileCont) {
		// TODO Auto-generated method stub
		return null;
	}

	/** 
	 *  从接收到的文件中判断是否有已存在的记录
	 */
	public boolean alreadExist() {
		
		// 接口文件中审批编号是开头的20位
		for(int i = 1; i< this.fileCont.size(); i++){
			String contCode = this.fileCont.get(i).substring(0, 20);
			if(alreadExist(contCode)){
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 根据 中心识别码判断记录是否已存在数据库中
	 * @param contCode String
	 * @return
	 * 
	 * boolean
	 */
	@Override
	public boolean alreadExist(String contCode){
		LoanAuthService las = (LoanAuthService) SpringContextUtil.getBean("LoanAuthService");
		return las.isExist(contCode);
	}
	
	/**
	 * 审批表中分记录的中心识别码是否已存在数据库中
	 * @param line String
	 * @return boolean
	 */
	public boolean alreadExistEach(String line) {
		String contCode = line.substring(0, 20);
		
		return alreadExist(contCode);
		
	}
	
	/**
	 * 授权文件中头部的总记录格式校验. 		
	 * @作者   		liqing
	 * @创建日期 	2012-11-13 22:51 
	 * @param fileCont List<String>
	 * @throws none
	 * @return boolean
	 */
	public  boolean isHeadRight(List<String> fileCont) {
		/*
		 * 文件中的第一行信息为登记贷款授权总笔数+总金额，格式占21字符
		 */
		if (fileCont.get(0).length() == 21) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 授权文件中分记录的格式是否正确
	 * @param line String
	 * @return boolean 
	 */
	public boolean isFormatRtEach(String line){
		//一条分记录 的总长是90字节
		if(90 != line.length() || !"DK".equals(line.substring(0, 2))){
			return false;
		}
		return true;
	}

}
